package sk.tuke.magsa.personalistika.dao_impl;

import java.sql.Connection;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
import sk.tuke.magsa.framework.CRUDDaoImpl;
import sk.tuke.magsa.framework.ValidatorException;
import sk.tuke.magsa.framework.ConnectionPool;
import sk.tuke.magsa.personalistika.entity.*;
//import sk.tuke.magsa.personalistika.entity.Zamestnanec;
import sk.tuke.magsa.personalistika.dao.ZamestnanecDao;
import sk.tuke.magsa.personalistika.Application;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class ZamestnanecDaoImpl extends CRUDDaoImpl<Zamestnanec> implements ZamestnanecDao {
    public ZamestnanecDaoImpl(ConnectionPool pool) {
        super(pool);
    }    

    protected PreparedStatement prepareInsertStatement(Connection connection, Zamestnanec object) throws SQLException {
        String query = "INSERT INTO Zamestnanec (meno,priezvisko,vek, ident_Oddelenie) VALUES (?, ?, ?, ?)";
        PreparedStatement pstm = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
        pstm.setString(1, object.getMeno());
        pstm.setString(2, object.getPriezvisko());
        pstm.setInt(3, object.getVek());
        pstm.setInt(4, object.getOddelenie());
        test(object);
        return pstm;
    }

    protected PreparedStatement prepareUpdateStatement(Connection connection, Zamestnanec object) throws SQLException {
                String query = "UPDATE Zamestnanec SET meno = ?,  priezvisko = ?,  vek = ? , ident_Oddelenie = ? WHERE ident = ?";
        PreparedStatement pstm = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
        pstm.setString(1, object.getMeno());
        pstm.setString(2, object.getPriezvisko());
        pstm.setInt(3, object.getVek());
        pstm.setInt(4, object.getOddelenie());
        pstm.setInt(5, object.getIdent());

        return pstm;
    }

    protected PreparedStatement prepareDeleteStatement(Connection connection, Zamestnanec object) throws SQLException {
        String query = "DELETE FROM Zamestnanec WHERE meno = ? AND priezvisko = ? AND vek = ? ";
        PreparedStatement pstm = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
        pstm.setString(1, object.getMeno());
        pstm.setString(2, object.getPriezvisko());
        pstm.setInt(3, object.getVek());

        return pstm;
    }

    protected PreparedStatement prepareFindStatement(Connection connection, Integer id) throws SQLException {
        String query = "SELECT * FROM Zamestnanec WHERE ident = ? ";
        PreparedStatement pstm = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
        pstm.setInt(1, id);


        return pstm;

    }

    protected PreparedStatement prepareSelectStatement(Connection connection) throws SQLException {
         String query = "SELECT * FROM Zamestnanec";
         PreparedStatement pstm = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
         return pstm;
         
    }

    protected Zamestnanec createFromResultSet(ResultSet rs) throws SQLException {
        Zamestnanec result = new Zamestnanec();
        result.setIdent(rs.getInt("ident"));
        result.setMeno(rs.getString("meno"));
        result.setPriezvisko(rs.getString("priezvisko"));
        result.setVek(rs.getInt("vek"));
        result.setOddelenie(rs.getInt("ident_Oddelenie"));
        return result;
    }

    public List<Zamestnanec> findString(String name) {
        List<Zamestnanec> newList = new ArrayList<Zamestnanec>();
        List<Zamestnanec> list = super.selectAll();
        
        for (Zamestnanec zamestnanec : list )
        {
                if(zamestnanec.getMeno().toString().contains(name))
                {
                    newList.add(zamestnanec);
                    break;
                }
                if(zamestnanec.getPriezvisko().toString().contains(name))
                {
                    newList.add(zamestnanec);
                    break;
                }
                if(zamestnanec.getVek().toString().contains(name))
                {
                    newList.add(zamestnanec);
                    break;
                }
                    }
            return newList;
    }

    public List<Zamestnanec> findByProperty(String name, int i) {
        List<Zamestnanec> newList = new ArrayList<Zamestnanec>();
        List<Zamestnanec> list = super.selectAll();
                for (Zamestnanec zamestnanec : list )
        {
            switch(i)
            {
                case 0:if(zamestnanec.getMeno().contains((String)name))
                {
                    newList.add(zamestnanec);
                    break;
                }
            break;
                case 1:if(zamestnanec.getPriezvisko().contains((String)name))
                {
                    newList.add(zamestnanec);
                    break;
                }
            break;
                case 2:if(zamestnanec.getVek() == Integer.valueOf((String)name))
                {
                    newList.add(zamestnanec);
                    break;
                }
            break;
                case 3:List<Oddelenie> listO = Application.getInstance().getOddelenieDao().findByProperty(name, 0);
    if(listO.size() > 0)
    {
        if(zamestnanec.getOddelenie() == listO.get(0).getIdent())
        {
            newList.add(zamestnanec);
            break;
        }
    }    
    break;  
            }
        }
        return newList;
    }

    @Override
    protected void test(Zamestnanec object) {
     
//-------------------------------Required-------------------------------
        if(object.getMeno() == null) {
             throw new ValidatorException("Property 'Meno' is required!");
            }
    
//-------------------------------LENGTH-------------------------------
        if(object.getMeno() != null) {
            if( object.getMeno().length() > 30 || object.getMeno().length() < 2) {
                throw new ValidatorException("Property 'Meno' has length out of range!");
            }
        }
     
//-------------------------------Required-------------------------------
        if(object.getPriezvisko() == null) {
             throw new ValidatorException("Property 'Priezvisko' is required!");
            }
    
//-------------------------------LENGTH-------------------------------
        if(object.getPriezvisko() != null) {
            if( object.getPriezvisko().length() > 30 || object.getPriezvisko().length() < 2) {
                throw new ValidatorException("Property 'Priezvisko' has length out of range!");
            }
        }
 


    }

    
}
